package org.example.struct.array;

/**
 * @author liyishan
 * @date 2024/2/19 09:43
 * @apiNote
 */

public class Test00844BackspaceCompare {
    public static void main(String[] args) {
        String s = "ab#c", t = "ad#d";
        System.out.println(backspaceCompare(s, t));
    }

    public static boolean backspaceCompare(String s, String t) {
        String newS = process(s);
        String newT = process(t);
        return newS.equals(newT);
    }
    private static String process(String s) {
        StringBuilder sb = new StringBuilder();
        for(char c : s.toCharArray()){
            if(c == '#'){
                if(sb.length() > 0){
                    sb.deleteCharAt(sb.length() - 1);
                }
            }else{
                sb.append(c);
            }
        }
        return sb.toString();
    }
}
